<!-- ************************************************************************************************************ -->
<!-- fileName          : loginAction.jsp 																		  -->
<!-- author            : 马文军 																					  --> 
<!-- createDate        : 2014/12/21 																			  -->
<!-- summary           : 登陆操作 后台逻辑															  					  -->
<!-- modifiedBy        : 马文军																				      -->
<!-- ************************************************************************************************************ -->

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>loginAction</title>
</head>
<body>
<%
	//设置请求的字符编码以支持中文
	request.setCharacterEncoding ("utf-8");

	//获取请求参数
	String username = request.getParameter("username");
	String password = request.getParameter("password");

	//从web.xml获取全局参数
	String drv=config.getServletContext().getInitParameter("DBDriver");
	String url=config.getServletContext().getInitParameter("DBURL");
	String uid=config.getServletContext().getInitParameter("DBUser");
	String pwd=config.getServletContext().getInitParameter("DBPass");
	
	//构建SQL字符串和JDBC对象
	String strSQL = "SELECT username,password FROM immie0_immi.user u WHERE u.username=? and u.password=?";
	Connection conn = null;
	PreparedStatement pstmt = null;	
	ResultSet rs = null;
	
	try {
		Class.forName(drv).newInstance(); //数据库驱动加载
		conn = DriverManager.getConnection(url,uid,pwd);//取得数据库连接
		pstmt = conn.prepareStatement(strSQL);		//实例化数据库操作对象
		//设置SQL语句的参数
		pstmt.setString(1, username);	
		pstmt.setString(2, password);	
		rs = pstmt.executeQuery();  //执行查询

		if(rs.next()) 
		{
			// ！！！！！！ 这里可以根据数据库获取的信息进行分类   以后可以    对不同账户设置权限或者进入不同页面 ！！！！！！！！！！！！！！
			//获取结果集    中的用户名和密码 ，并将其存入session
			//String user_name = rs.getString(1);
			//String user_password = rs.getString(2);

			//session.setAttribute("user_name", user_name);
			//session.setAttribute("user_password", user_password);

			
			//如果用户名不为空  则表示信息正确    成功登陆
			//if ( !user_name.equals("超级管理员")) {  
			//	response.sendRedirect("/immie/page/index.jsp");
			//}
			response.sendRedirect("/immigration/page/admin/admin_index.jsp");
		}
		else
			response.sendRedirect("/immigration/loginFail.do");
		
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (InstantiationException e) {
	    e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	} 
	finally {
		if (rs != null) {
			rs.close();
			rs = null;
		}
		if (pstmt != null) {
			pstmt.close();
			pstmt = null;
		}		
		if (conn != null) {
			conn.close();
			conn = null;
		}
	}
%>
</body>
</html>